/**
  * <br>JAVACC DEMO 1.0<br>
  * @copy right iussoft company All rights reserved.<br>
  * <br>
  * @Package com.apache.dict.controller
*/
package com.apache.dict.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.api.vo.Token;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.dict.entity.DataCate;
import com.apache.dict.entity.DataItem;
import com.apache.dict.manager.DataCateManager;
import com.apache.dict.manager.DataItemManager;
import com.apache.uct.common.BaseAction;

/**
  * description: 数据字典实体action
  * @author Hou DaYu 创建时间：上午08:34:22
  */
@Controller
@RequestMapping("/dict/cate/")
public class DataCateAction extends BaseAction<DataCate> {

	/**
	 * 数据字典的Manager
	 */
	@Autowired
	DataCateManager dataCateManager;
	/**
	 * 数据字典条目管理的Manager
	 */
	@Autowired
	DataItemManager dataItemManager;

	/**
	 * 功能说明：通用跳转到新增页面
	 */
	@Token(save = true)
	@RequestMapping("add.action")
	protected ModelAndView add(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(ADD);
		DataCate dataCate = new DataCate();
		dataCate.setCateStatus("1");
		view.addObject("isAdd", "true");
		view.addObject("item", dataCate);
		return view;
	}

	/**
	 * 功能说明：通用跳转到编辑页面
	 */
	@Token(save = true)
	@RequestMapping(value = "/edit.action")
	public ModelAndView edit(HttpServletRequest request, String id) {
		ParamsVo<DataCate> vo = new ParamsVo<DataCate>();
		vo.setInfoId(id);
		ModelAndView view = new ModelAndView(EDIT);
		view.addObject("item", getBaseManager().getInfoById(vo));
		view.addObject("isAdd", "false");
		return view;
	}

	@Token(remove = true)
	@RequestMapping("save.action")
	public @ResponseBody
	ResultMsg save(HttpServletRequest request, HttpServletResponse response, DataCate dataCate) {
		ResultMsg rmsg = new ResultMsg();
		rmsg.setFlag("T");
		rmsg.setMsg("操作成功！");
		String cateId = dataCate.getCateId();
		if (Validator.isNull(dataCate.getCateId())) {//新增
			ParamsVo<DataCate> vo = this.getParamsVo(request);
			vo.setMethodKey("checkCateEname");
			vo.setParams("cateEname", dataCate.getCateEname());
			Object obj = dataCateManager.execute(vo);
			if ("true".equals(obj.toString())) {
				//				dataCate.setCreateUser("");
				//				dataCate.setSysEname("");
				vo = new ParamsVo<DataCate>();
				vo.setObj(dataCate);
				cateId = dataCateManager.saveInfo(vo);
				if (Validator.isNull(cateId)) {
					rmsg.setFlag("F");
					rmsg.setMsg("添加科目失败，请重试！");
				}
			} else {
				rmsg.setFlag("H");
				rmsg.setMsg("科目标识已被使用，请核对！");
			}
		} else {//修改
			String oldCateEname = request.getParameter("oldCateEname");
			ParamsVo<DataCate> vo = this.getParamsVo(request);
			if (Validator.isNotNull(oldCateEname)) {
				ParamsVo<DataItem> itemvo = new ParamsVo<DataItem>();
				itemvo.setMethodKey("checkDataItemByFatherId");
				itemvo.setParams("cateEname", dataCate.getCateEname());
				itemvo.setParams("fatherId", "0");
				Object obj = dataItemManager.execute(itemvo);
				if ("true".equalsIgnoreCase(obj.toString())) {
					rmsg.setFlag("H");
					rmsg.setMsg("该科目下存在条目列表，科目标识不能修改！");
					return rmsg;
				}
			}
			vo = new ParamsVo<DataCate>();
			vo.setObj(dataCate);
			dataCateManager.editInfo(vo);
		}
		return rmsg;
	}

	/**
	 * 获取分页列表，数据字典查询
	 */
	@RequestMapping("list!data.action")
	@ResponseBody
	protected Object data(HttpServletRequest request, String page, String rows) {
		String cateEname = request.getParameter("cateEname");
		String cateCname = request.getParameter("cateCname");
		String cateStatus = request.getParameter("cateStatus");
		ParamsVo<DataCate> vo = new ParamsVo<DataCate>();
		vo.setParams("pageSize", Validator.getDefaultStr(rows, "10"));
		vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
		vo.setParams("cateEname", cateEname);
		vo.setParams("cateCname", cateCname);
		if (Validator.isNotNull(cateStatus))
			vo.setParams("cateStatus", cateStatus);
		else {
			vo.setParams("cateStatus", "1");
		}
		Page pageList = dataCateManager.getPageInfo(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("total", pageList.getCount());
		mapData.put("rows", pageList.getPageObjects());
		return mapData;
	}

	/**
	 * 删除数据字典
	 */
	@RequestMapping("del.action")
	public @ResponseBody
	ResultMsg delete(HttpServletRequest request, String id) {
		String cateEname = request.getParameter("cateEname");
		ResultMsg rmsg = new ResultMsg();
		rmsg.setFlag("T");
		rmsg.setMsg("操作成功！");
		if (Validator.isNotNull(cateEname)) {
			ParamsVo<DataItem> itemvo = new ParamsVo<DataItem>();
			itemvo.setMethodKey("checkDataItemByFatherId");
			itemvo.setParams("cateEname", cateEname);
			itemvo.setParams("fatherId", "0");
			Object result = dataItemManager.execute(itemvo);
			if ("true".equals(result.toString())) {
				rmsg.setFlag("H");
				rmsg.setMsg("请先删除该科目下的条目列表！");
				return rmsg;
			}
		}
		if (Validator.isNotNull(id)) {
			ParamsVo<DataCate> vo = this.getParamsVo(request);
			vo.setInfoId(id);
			dataCateManager.deleteInfo(vo);
		}
		return rmsg;
	}

	@Override
	protected BaseManager<DataCate> getBaseManager() {
		return dataCateManager;
	}

	@Override
	protected String getPrefix() {
		return "dict/mvc/data-cate-";
	}

}
